<?php

namespace app\admin\controller;

use app\common\business\AdminRole;
use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class Role extends AdminBase
{
    /**
     * 管理后台列表
     * @return Json
     */
    public function list(): Json
    {
        list($roleName, $page,$limit,$status) = Param::getMore([
            ['roleName', ''],
            ['page', null],
            ['limit', null],
            ['status', null],
        ], $this->request, true);
        try {
            $list=(new AdminRole())->getTableList($page,$limit,$roleName,$status);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($list);
    }

    /**
     * 根据角色ID获取权限树及选中状态
     * @return Json
     */
    public function treeById(): Json
    {
        $id=$this->request->get('id');
        try {
            $data=(new AdminRole())->getRoleTreeById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }

    /**
     * 新增/保存数据
     * @return Json
     */
    public function save(): Json
    {
        list($id, $status,$rules,$roleName) = Param::postMore([
            ['id',null],
            ['status', 1],
            ['rules', ''],
            ['role_name', ''],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
            'rules'=>$rules,
            'role_name'=>$roleName
        ];
        $validate = new \app\admin\validate\Role();
        $scene=empty($data['id'])?'create':'update';
        if (!$validate->scene($scene)->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new AdminRole())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 修改状态
     * @return Json
     */
    public function updateStatus(): Json
    {
        list($id, $status) = Param::getMore([
            ['id',null],
            ['status', null],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
        ];
        try {
            $result = (new AdminRole())->updateStatus($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 根据id删除
     * @return Json
     */
    public function deleteById(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new AdminRole())->deleteById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "删除成功");
        }
        return Show::error([], '删除失败');
    }
}
